<?php
	namespace Api\Controller;
	use Think\Controller;
	header("Content-Type:text/html;charset=utf-8");
	class FansController extends Controller
	{
		/* 
		 *关注某位艺人、企业或者对某位艺人、企业取消关注 
		 *  
		 */
		public function attention($uid=null,$uids=null,$token=null)
		{
			if ($uid==null || $uids == null) 
			{
				$array['result'] = 10301;
				$array['message'] = "关注失败";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			elseif ($this->checkedToken($uid,$token))
			{
				$where['uid'] = $uid;
				$fansinfo = M("User") -> field("attention") -> where($where) ->find();
				//var_dump($fansinfo);
				if ($fansinfo["attention"] != null) 
				{
					$array = explode(",", $fansinfo["attention"]);
					
					if (in_array($uids, $array)) 
					{
						//$attentionPeople = explode(",", $attentionUid["attention"]);
							
						$key = array_search($uids, $array);
						if ($key !== false)
						{
							array_splice($array, $key, 1);
						}
						$attentionString = implode(",", $array);
						//var_dump($attentionString);
						$cancelResult = M("User") -> where($where) -> setField("attention", $attentionString);
						
						if ($cancelResult > 0)
						{
							$array1['result'] = 0;
							$array1['message'] = "取消关注成功";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						else
						{
							$array1['result'] = 10311;
							$array1['message'] = "取消关注失败";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						
// 						$array1['result'] = 10302;
// 						$array1['message'] = "已关注该用户";
// 						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
// 						exit;
					}
					else
					{
						$condition["uid"] = $uids;
						$uidsAttention = M("User") -> field("attention") -> where($condition) -> find();
						if ($uidsAttention == null) 
						{
							//$uids用户未关注该登录用户
							$mutual = 0;
						}
						else
						{
							$uidsAttentionArray = explode(",", $uidsAttention["attention"]);
							if (in_array($uid, $uidsAttentionArray)) 
							{
								//$uids用户已关注过登录用户
								$mutual = 1;
							}
							else
							{
								//$uids用户未关注该登录用户
								$mutual = 0;
							}
						}
						array_push($array, $uids);
						$uidstring = implode(",", $array);
						//对数据库中的user表的fans
						$res = M("User") -> where($where) ->setField("attention", $uidstring);
						
						if ($res > 0) 
						{
							$array1['result'] = 0;
							$array1['message'] = "关注成功";
							$array1["mutual"] = $mutual;
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						else
						{
							$array1['result'] = 10303;
							$array1['message'] = "关注失败";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
					}
					
				}
				else
				{
					//查询要关注的用户是否已关注过自己
					$condition1["uid"] = $uids;
					$uidsAttention = M("User") -> field("attention") -> where($condition1) -> find();
					if ($uidsAttention == null) 
					{
						//$uids用户未关注该登录用户
						$mutual = 0;
					}
					else
					{
						$uidsAttentionArray = explode(",", $uidsAttention["attention"]);
						if (in_array($uids, $uidsAttentionArray))
						{
							//$uids用户已关注过登录用户
							$mutual = 1;
						}
						else
						{
							//$uids用户未关注该登录用户
							$mutual = 0;
						}
					}
					//向user表中添加第一个关注的人
					$where["uid"] = $uid;
					/* $uids = $uids."-"; */
					$result = M("User") -> where($where) ->setField("attention", $uids);
					
					if ($result > 0)
					{
						$array1['result'] = 0;
						$array1['message'] = "关注成功";
						$array1["mutual"] = $mutual;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array1['result'] = 10304;
						$array1['message'] = "关注失败";
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
			}
			else
			{
				$array['result'] = 10310;
				$array['message'] = "关注失败";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
		
		
		/*
		 *查询已关注的人/企业
		 *
		 */
		public function attentionPeople($uid=null, $token=null, $page=null)
		{
// 			if ($this->checkedToken($uid,$token)) 
// 			{
				$where["uid"] = $uid;
				$attentioninfo = M("User") -> field("attention") -> where($where) ->find();
				
				//关注的总人数
				$uids = explode(",", $attentioninfo["attention"]);
				$attentionNumber = count($uids);
				//分页
// 				$currentpage = isset($page) ? $page:1;
			
// 				$pagesize    = 15;
				
// 				if ($attentionNumber > $pagesize) 
// 				{
// 					$pagecount = ceil($attentionNumber/$pagesize);
// 				}
// 				else
// 				{
// 					$pagecount = 1;
// 				}
				
// 				//var_dump($pagecount);
// 				if ($pagecount < $page)
// 				{
// 					$array['result'] = -2;
// 					$array['message'] = "没有更多的数据了！";
// 					echo json_encode($array, JSON_UNESCAPED_SLASHES);
// 					exit;
// 				}
				
// 				$start       = ($currentpage-1)*$pagesize;// 分页显示输出
				
			
				
				//var_dump($attentioninfo);
				if ($attentioninfo["attention"] != null) 
				{
					//将查询得到的字符串转化为数组
					$uids = explode(",", $attentioninfo["attention"]);
					//根据查询到的关注的uid的数组去查询注册表和用户信息表
					$condition["uid"] = array("in", $uids);
					$userinfo = M("User") -> where($condition) -> select();
					
					//var_dump($userinfo);
					foreach ($userinfo as $key=>$value)
					{
						if ($value["type"] == 0) 
						{
							if ($value["rmtype"]==0)
							{ 
// 								unset($userinfo[$key]);
								$uid1[] = $value["uid"];
							}
							else
							{
								$uid2[] = $value["uid"];
							}
						}
						else
						{
							if ($value["rmtype"]==0)
							{
// 								unset($userinfo[$key]);
								$uid4[] = $value["uid"];
							}
							else
							{
								$uid5[] = $value["uid"];
							}
						}
					}
					
// 					$uid3 = array_diff($uid2, $uid1);
					
// 					var_dump($uid1);
// 					var_dump($uid2);
// 					if ($uid4==null || $uid5==null)
// 					{
// 						$uid6 = array_diff($uid5, $uid4);
// 					}
// 					elseif ($uid4 == null)
// 					{
// 						$uid6 = $uid5;
// 					}
					
					//$uid6 = array_diff($uid5, $uid4);
					//var_dump($uid1);
					//var_dump($uid2);
					if ($uid1 != null) 
					{
						$condition1["uid"] = array("in", $uid1);
						$userinfo2 = M("User") -> field("uid,type,nickname,attention,dtttype") -> where($condition1) -> select();
						foreach ($userinfo2 as $key=>$value)
						{
							//将查询得到的字符串转化为数组
							if ($value["attention"] == null) 
							{
								unset($value["attention"]);
								$value["mutual"] = 0;
							}
							else
							{
								$uids9 = explode(",", $value["attention"]);
								unset($value["attention"]);
								if (in_array($uid, $uids9)) 
								{
									$value["mutual"] = 1;
								}
								else
								{
									$value["mutual"] = 0;
								}
							}
							$value["sex"] = "3";
							$value["headimg"] = "";
							$value["tagname"] = array();
							$userinfo2[$key] = $value;
// 							unset($userinfo2[$key]["attention"]);
						}
					}
					else
					{
						$userinfo2 = null;
					}
					
					if ($uid2 != null) 
					{
						
					
						$condition2["think_user.uid"] = array("in", $uid2);
						//$userinfo3 = M("User") -> where($condition2) -> select();
						$userinfo3 = M("User") -> join("__USERINFO__ on __USER__.uid=__USERINFO__.uid") -> field("think_user.uid,type,dtttype,attention,sex,nickname,tagname,headimg") -> where($condition2) -> order("rmtype desc,dtttype desc") -> select();
						foreach ($userinfo3 as $k3=>$v3)
						{	
							//将查询得到的字符串转化为数组
							if ($v3["attention"] == null)
							{
								unset($v3["attention"]);
								$v3["mutual"] = 0;
							}
							else
							{
								$uids7 = explode(",", $v3["attention"]);
								unset($v3["attention"]);
								if (in_array($uid, $uids7))
								{
// 									$userinfo3[$k3]["mutual"] = 1;
									$v3["mutual"] = 1;
								}else
								{
// 									$userinfo3[$k3]["mutual"] = 0;
									$v3["mutual"] = 0;
								}
							}
							if ($v3['tagname'] == null) 
							{
								$v3['tagname'] = array();
							}else
							{
								$tagname = explode("-", $v3['tagname']);
								unset($v3['tagname']);
								$v3["tagname"] = $tagname;
							}
							$userinfo3[$k3] = $v3;
							
// 							unset($userinfo3[$k3]["attetion"]);
						}
					}
					else
					{
						$userinfo3 = null;
					}
					
					if ($uid4 != null)
					{
						$condition4["uid"] = array("in", $uid4);
						$userinfo4 = M("User") -> field("uid,type,nickname,attention,dtttype") -> where($condition4) -> select();
						foreach ($userinfo4 as $key=>$value)
						{
							//将查询得到的字符串转化为数组
							if ($value["attention"] == null) 
							{
								unset($value["attention"]);
								$value["mutual"] = 0;
							}
							else
							{
								$uids1 = explode(",", $value["attention"]);
								unset($value["attention"]);
								if (in_array($uid, $uids1))
								{
									$value["mutual"] = 1;
								}else
								{
									$value["mutual"] = 0;
								}
							}
							
							$value["sex"] = "0";
							$value["headimg"] = "";
							$value["tagname"] = array();
							$userinfo4[$key] = $value;
						}
					}
					else
					{
						$userinfo4 = null;
					}
					if ($uid5 != null)
					{
					
							
						$condition6["think_user.uid"] = array("in", $uid5);
						//$userinfo3 = M("User") -> where($condition2) -> select();
						$userinfo6 = M("User") -> join("__COMPANY__ on __USER__.uid=__COMPANY__.uid") -> field("think_user.uid,type,attention,dtttype,nickname,tagname,cplogo") -> where($condition6) -> order("rmtype desc,dtttype desc") -> select();
						foreach ($userinfo6 as $k=>$v)
						{
							//将查询得到的字符串转化为数组
							if ($v["attention"] == null)
							{
								unset($v["attention"]);
								$v["mutual"] = 0;
							}
							else
							{
								$uids8 = explode(",", $v["attention"]);
								unset($v["attention"]);
								if (in_array($uid, $uids8))
								{
									$v["mutual"] = 1;
								}else
								{
									$v["mutual"] = 0;
								}
							}
					
							if ($v['tagname'] == null) 
							{
								$v["tagname"] = array();	
							}
							else
							{
								$tagname = explode("-", $v['tagname']);
								unset($v['tagname']);
								$v["tagname"] = $tagname;
							}
							$v["sex"] = "0";
							$userinfo6[$k] = $v;
							
						}
					}
					else
					{
						$userinfo6 = null;
					}
					
					if ($userinfo2 == null && $userinfo3 == null) 
					{
						$attentionpeople1 = null;
					}
					elseif ($userinfo2 == null)
					{
						$attentionpeople1 = $userinfo3;
					}
					elseif ($userinfo3 == null)
					{
						$attentionpeople1 = $userinfo2;
					}
					else
					{
						$attentionpeople1 = array_merge($userinfo3, $userinfo2);
					}
					
					if ($userinfo4 == null && $userinfo6 == null) 
					{
						$attentionpeople2 = null;
					}
					elseif ($userinfo4 == null)
					{
						$attentionpeople2 = $userinfo6;
					}
					elseif ($userinfo6 == null)
					{
						$attentionpeople2 = $userinfo4;
					}
					else
					{
						$attentionpeople2 = array_merge($userinfo6, $userinfo4);
					}
					
					if ($attentionpeople1 == null && $attentionpeople2) 
					{
						$attentionpeople = null;
					}
					elseif ($attentionpeople2 == null)
					{
						$attentionpeople = $attentionpeople1;
					}
					elseif ($attentionpeople1 == null)
					{
						$attentionpeople = $attentionpeople2;
					}
					else
					{
						$attentionpeople = array_merge($attentionpeople1, $attentionpeople2);
					}
					foreach ($attentionpeople as $akey=>$avalue)
					{
						if ($avalue["sex"] == 0) 
						{
							$headimg = $avalue["cplogo"];
							unset($avalue["cplogo"]);
							$avalue["headimg"] = $headimg;
						}
						$attentionpeople[$akey] = $avalue;
					}
					
				    $currentpage = isset($page) ? $page:1;
				    $size=15;//每页显示的记录数
                 	$fansNumber = count($attentionpeople);
                 	if ($fansNumber < 15)
                 	{
		         	   $pnum = 1;
		        	}
             		else
             		{
						$pnum = ceil(count($attentionpeople) / $size); //总页数，ceil()函数用于求大于数字的最小整数
             		}
					
					if ($pnum < $page)
					{
						$array['result'] = 10320;
						$array['message'] = "没有更多的数据了！";
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
					//用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度)
					$newarr = array_slice($attentionpeople, ($page-1)*$size, $size);
					
					//var_dump($attentionpeople1);
					if ($attentionpeople != null)
					{
						$array1['result'] = 0;
						$array1['message'] = "查询关注的人成功";
						$array1['attentionpeople'] = $newarr;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array1['result'] = 10305;
						$array1['message'] = "查询关注的人失败";
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
				} 
// 			}
// 			else
// 			{
// 				$array['result'] = 10310;
// 				$array['message'] = "查询失败";
// 				echo json_encode($array, JSON_UNESCAPED_SLASHES);
// 				exit;
// 			}
		}
		
		/*
		 *查询被关注(粉丝)
		 *
		 */
		public function fansPeople($uid=null, $token=null, $page=null)
		{
// 			if ($this->checkedToken($uid,$token)) 
// 			{

				$condition["attention"] = array(
						array("like","$uid,%"),
						array("like","%,$uid"),
						array("like","%,$uid,%"),
						array("like",$uid),
						'ThinkPHP','OR'
				);
				
				$uids = M("User") -> field("uid") -> where($condition) -> select();
				//var_dump($uids);
				if ($uids == null) 
				{
					$array1['result'] = 10307;
					$array1['message'] = "该用户暂无粉丝";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				else
				{
					foreach ($uids as $v)
					{
						$uids1 .= $v["uid"].",";
						
					}
					//var_dump(explode(",", (rtrim($uids1,","))));
					$uids2 = explode(",", (rtrim($uids1,",")));
					
					$where["uid"] = array("in", $uids2);
					$fansInfo = M("User") -> where($where) -> select();
					//var_dump($fansInfo);
					foreach ($fansInfo as $k=>$v)
					{
						if ($v["type"] == 0) 
						{
							//个人
							if ($v["rmtype"] == 0) 
							{
								$uids3[$k] = $v["uid"];
							}
							else
							{
								$uids4[$k] = $v["uid"];
							}
							 
						}
						else
						{
							//企业
							if ($v["rmtype"] == 0) 
							{
								$uids5[$k] = $v["uid"];
							}
							else
							{
								$uids6[$k] = $v["uid"];
							}
						}
					}
				}
// 				var_dump($uids5);
// 				var_dump($uids6);
				if ($uids3 != null) 
				{
					$where1["uid"] = $uid; 
					$attention = M("User") -> where($where1) ->getField("attention");
					//将查询得到的字符串转化为数组
					$attentionuid = explode(",", $attention);
						
					$where2["uid"] = array("in", $uids3);
					$userinfo3 = M("User") -> field("uid,type,nickname,dtttype") -> where($where2) -> order("regtime desc")-> select();
					foreach ($userinfo3 as $key=>$value)
					{
						
						if (in_array($value["uid"], $attentionuid)) 
						{
							$userinfo3[$key]["mutual"] = 1;
						}
						else
						{
							$userinfo3[$key]["mutual"] = 0;
						}
						$userinfo3[$key]["headimg"] = "";
						$userinfo3[$key]["tagname"] = array();
					}
				}
				else
				{
					$userinfo3 = null;
				}
				
				if ($uids4 != null)
				{
					$where1["uid"] = $uid;
					$attention = M("User") -> where($where1) ->getField("attention");
					//将查询得到的字符串转化为数组
					$attentionuid = explode(",", $attention);
				
					$where3["think_user.uid"] = array("in", $uids4);
					$userinfo4 = M("User") -> join("__USERINFO__ on __USER__.uid=__USERINFO__.uid") -> field("think_user.uid,type,dtttype,sex,nickname,tagname,headimg") -> where($where3) -> order("rmtype desc,dtttype desc,regtime desc") -> select();
					foreach ($userinfo4 as $key4=>$value4)
					{
						if ($value4["headimg"] == null) 
						{
							$value4["headimg"] = "";
						}
				
						if ($value4['tagname'] == null) 
						{
							$value4['tagname'] = array();
						}
						else
						{
							$tagname = explode("-", $value4['tagname']);
							unset($value4['tagname']);
							$value4["tagname"] = $tagname;
						}
						$userinfo4[$key4] = $value4;
						
						if (in_array($value4["uid"], $attentionuid))
						{
							$userinfo4[$key4]["mutual"] = 1;
						}
						else
						{
							$userinfo4[$key4]["mutual"] = 0;
						}
					}
				}
				else
				{
					$userinfo4 = null;
				}
				
				if ($uids5 != null)
				{
					$where1["uid"] = $uid;
					$attention = M("User") -> where($where1) ->getField("attention");
					//将查询得到的字符串转化为数组
					$attentionuid = explode(",", $attention);
				
					$where4["uid"] = array("in", $uids5);
					$userinfo5 = M("User") -> field("uid,type,nickname,dtttype") -> where($where4) -> order("regtime desc") -> select();
					foreach ($userinfo5 as $key5=>$value5)
					{
						if (in_array($value5["uid"], $attentionuid))
						{
							$userinfo5[$key5]["mutual"] = 1;
						}
						else
						{
							$userinfo5[$key5]["mutual"] = 0;
						}
						$userinfo5[$key5]["headimg"] = "";
						$userinfo5[$key5]["tagname"] = array();
					}
				}
				else
				{
					$userinfo5 = null;
				}
				
				if ($uids6 != null)
				{
					$where1["uid"] = $uid;
					$attention = M("User") -> where($where1) ->getField("attention");
					//将查询得到的字符串转化为数组
					$attentionuid = explode(",", $attention);
				
					$where5["think_user.uid"] = array("in", $uids6);
					$userinfo6 = M("User") -> join("__COMPANY__ on __USER__.uid=__COMPANY__.uid") -> field("think_user.uid,type,attention,dtttype,nickname,tagname,cplogo") -> where($where5) -> order("rmtype desc,dtttype desc,regtime desc") -> select();
					foreach ($userinfo6 as $key6=>$value6)
					{
						if ($value6["tagname"] == null) 
						{
							$value6["tagname"] = array();
// 							$userinfo6[$k] = $value;
						}
						else
						{
							$tagname = explode("-", $value6['tagname']);
							unset($value6['tagname']);
							$value6["tagname"] = $tagname;
// 							$userinfo6[$k] = $value;
						}
						if ($value6["cplogo"] == null) 
						{
							unset($value6['cplogo']);
							$value6["headimg"] = "";
// 							$userinfo6[$k] = $value;
						}
						else
						{
							$headimg = $value6["cplogo"];
							unset($value6["cplogo"]);
							$value6["headimg"] = $headimg;
						}
						$userinfo6[$key6] = $value6;
						
						if (in_array($value6["uid"], $attentionuid))
						{
							$userinfo6[$key6]["mutual"] = 1;
						}
						else
						{
							$userinfo6[$key6]["mutual"] = 0;
						}
					}
				}
				else
				{
					$userinfo6 = null;
				}
				//var_dump($userinfo5);
				if ($userinfo3 == null && $userinfo4 == null) 
				{
					$fanspeople1 = null;
				}
				elseif ($userinfo3 == null)
				{
					$fanspeople1 = $userinfo4;
				}
				elseif ($userinfo4 == null)
				{
					$fanspeople1 = $userinfo3;
				}
				else
				{
					$fanspeople1 = array_merge($userinfo4, $userinfo3);
				}
				
				if ($userinfo5 == null && $userinfo6==null) 
				{
					$fanspeople2 = null;
				}
				elseif ($userinfo6 == null)
				{
					$fanspeople2 = $userinfo5;
				}
				elseif ($userinfo5 == null)
				{
					$fanspeople2 = $userinfo6;
				}
				else
				{
					$fanspeople2 = array_merge($userinfo6, $userinfo5);
				}
				
				if ($fanspeople1 == null && $fanspeople2 == null) 
				{
					$fanspeople = null;
				}
				elseif ($fanspeople2 == null)
				{
					$fanspeople = $fanspeople1;
				}
				elseif ($fanspeople1 == null)
				{
					$fanspeople = $fanspeople2;
				}
				else
				{
					$fanspeople = array_merge($fanspeople1, $fanspeople2);
				}
// 				var_dump($fanspeople);
// 				var_dump($userinfo5);
// 				var_dump($userinfo6);
				$currentpage = isset($page) ? $page:1;
				$size=15;//每页显示的记录数
             	$fansNumber = count($fanspeople);
             	if ($fansNumber < 15)
             	{
		         	$pnum = 1;
		        }
             	else
             	{
					$pnum = ceil(count($fanspeople) / $size); //总页数，ceil()函数用于求大于数字的最小整数
             	}
				
				if ($pnum < $page)
				{
					$array['result'] = 10320;
					$array['message'] = "没有更多的数据了！";
					echo json_encode($array, JSON_UNESCAPED_SLASHES);
					exit;
				}
				//用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度)
				$newarr = array_slice($fanspeople, ($page-1)*$size, $size);


				if ($fanspeople != null)
				{
					$array1['result'] = 0;
					$array1['message'] = "查询粉丝成功";
					$array1['fanspeople'] = $newarr;
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				else
				{
					$array1['result'] = 10306;
					$array1['message'] = "查询粉丝失败";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
// 			}
// 			else
// 			{
// 				$array['result'] = 10310;
// 				$array['message'] = "查询失败";
// 				echo json_encode($array, JSON_UNESCAPED_SLASHES);
// 				exit;
// 			}
		}
		
		/*
		 *验证token的方法 
		 *   
		 */
		protected function checkedToken($uid = null,$token = null)
		{
			if ($uid==null || $token==null) 
			{
				return false;
			}
			else
			{
				$where["uid"] = $uid;
				$tokeninfo = M("User") -> where($where) -> getField("token");
				if ($token == $tokeninfo) 
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
	}